import {getUserFromJwt} from '~/utils/serveCommon'
import {createTag,findSameNameTag} from '~/server/database/repositories/tag'
export default defineEventHandler(async(context)=>{
  try{
    const {name,sign,kind} = await readBody(context) || {} // 读取参数
    if(!name || !sign || !kind){
      return sendError(context,createError({
        statusCode: 400,
        statusMessage: '参数错误!',
      }))
    }
    const user_id = getUserFromJwt(context)
    const hasSame = await findSameNameTag({name,sign,kind,user_id})
    if(hasSame){
      return sendError(context, createError({
        statusCode: 400,
        statusMessage: '已存在同类同名标签名称!',
      }))
    }
    const tag = await createTag({name,sign,kind,user_id})
    if (!tag) {
      return sendError(context, createError({
        statusCode: 400,
        statusMessage: '创建标签失败!',
      }))
    }
    return { resource: tag, msg:'创建标签成功!'}
  }
  catch(error){
    return sendError(context, createError('标签新增失败！'))
  }
})